# -*- coding: utf-8 -*-
# created on 2017/2/15

from mathsolver.functions.base import *
from sympy import *
from mathsolver.functions.yuanzhui.property import HyperbolaCoeffs
from mathsolver.functions.zhixian.lineOthers import ZhiXian007
from mathsolver.functions.shuyushi.compute import ShuYuShiCompute


class ReturnGraph(BaseFunction):
    def solver(self, *args):
        lie = args[0]
        syms = lie.free_symbols()
        unknown_symbols = set(syms).difference([Symbol("x"), Symbol("y")])
        self.steps.append(["方程为：", ""])
        if unknown_symbols:
            f1, f2 = lie.sympify()
            # f = f1 - f2
            # stepsolver = shuyushi_compute(known=self.known).solver(BasePoly(f))
            if len(unknown_symbols) > 1:
                ans = self.search_multiple(unknown_symbols)
            else:
                ans = self.search(list(unknown_symbols)[0])
            if isinstance(ans, FiniteSet):
                for values in ans:
                    if isinstance(values, (tuple, Tuple)):
                        symbol_value = dict(
                            [(sym, v) for sym, v in zip(unknown_symbols, values) if not isinstance(v, Set)])
                    elif not isinstance(values, Set):
                        symbol_value = {list(unknown_symbols)[0]: values}
                    else:
                        raise ValueError()
                    eq = [f1.subs(symbol_value), f2.subs(symbol_value)]
                    lie.value = eq
                    self.steps.append([lie.printing(), ""])

            elif isinstance(ans, PieceWise):
                for values, _ in ans.pieces:
                    if isinstance(values, (tuple, Tuple)):
                        symbol_value = dict(
                            [(sym, v) for sym, v in zip(unknown_symbols, values) if not isinstance(v, Set)])
                    elif not isinstance(values, Set):
                        symbol_value = {list(unknown_symbols)[0]: values}
                    else:
                        raise ValueError()

                    eq = [f1.subs(symbol_value), f2.subs(symbol_value)]
                    lie.value = eq
                    self.steps.append([lie.printing(), ""])
        else:
            self.steps.append([lie.printing(), ""])

        self.output.append(lie)
        return self


class ReturnPoint(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        if args[0].free_symbols():
            if f1.free_symbols:
                f1 = ShuYuShiCompute(known=self.known).solver(BasePoly(f1)).output[0].sympify()

            if f2.free_symbols:
                f2 = ShuYuShiCompute(known=self.known).solver(BasePoly(f2)).output[0].sympify()

            args[0].value = [f1, f2]

        self.steps.append([args[0].printing(), ""])
        self.output.append(args[0])
        return self


class BiaoShi(BaseFunction):
    def solver(self, *args):
        old_a, old_b = HyperbolaCoeffs().solver(args[0]).output[0].sympify()
        a, b = HyperbolaCoeffs().solver(args[1]).output[0].sympify()
        self.output.append(BaseEqs([[a, old_a], [b, old_b]]))
        args[1].value = args[0].value
        self.output.append(args[1])
        return self


class ZhiXiansIsEq(BaseFunction):
    def solver(self, *args):
        zhixians, eq = args[:2]

        if "zhenf" in "".join([str(v) for v in eq.value]):
            f1, f2 = eq.sympify()
            zhixian1 = [(f1 - f2).subs({Symbol("zhenf"): 1}), 0]
            zhixian2 = zhixians.sympify()[0]
            return ZhiXian007().solver(BaseEq(zhixian1), BaseEq(zhixian2))
        elif str(eq.value[1])[0] == "-":
            zhixian2 = zhixians.sympify()[1]
            return ZhiXian007().solver(eq, BaseEq(zhixian2))
        else:
            zhixian2 = zhixians.sympify()[0]
            return ZhiXian007().solver(eq, BaseEq(zhixian2))


class ZhiXianJiaJiao(BaseFunction):
    def solver(self, *args):
        a1, b1, c1 = args[0].value
        a2, b2, c2 = args[1].value
        self.output.append(BasePoly(acos(abs(a1 * a2 + b1 * b2) / sqrt((a1 ** 2 + b1 ** 2) * (a2 ** 2 + b2 ** 2)))))
        self.label.add("两直线夹角")
        return self


class ZhiXianVariableUpdate(BaseFunction):
    def solver(self, *args):
        zhixian, variable = args[:2]
        zhixian.name = variable.value
        self.output.append(zhixian)
        return self


class ChuiXianAxisPoints(BaseFunction):
    def solver(self, *args):
        p1, p2 = args[0].sympify()
        axis = args[1].value
        p3, p4 = args[2].sympify()
        eqs = []
        if str(axis[0]) == "y":
            eqs.append([p3[1], 0])
            eqs.append([p4[1], 0])
            eqs.append([p3[0], p1[0]])
            eqs.append([p4[0], p2[0]])
        elif str(axis[0]) == "x":
            eqs.append([p3[0], 0])
            eqs.append([p4[0], 0])
            eqs.append([p3[1], p1[1]])
            eqs.append([p4[1], p2[1]])

        obj = BaseEqs(eqs)
        self.output.append(obj)
        self.steps.append(["由题意得，%s的坐标满足" % args[2].printing(), ""])
        self.steps.append([obj.printing(), ""])
        self.label.add("垂线与坐标轴相交")

        return self


class NotPingXing(BaseFunction):
    def solver(self, *args):
        a, b, c = args[0].value
        self.output.append(BaseIneqs([[a, "!=", 0], [b, "!=", 0]]))
        self.label.add("两直线位置关系")
        return self


class GetFirst(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BaseZhiXians):
            self.output.append(BaseZhiXian(args[0].value[0]))
        return self


if __name__ == '__main__':
    pass
